package com.example.webdemo;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.ComponentActivity;

import com.example.webdemo.utils.LogUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebViewActivity2 extends ComponentActivity implements View.OnClickListener {
    private RelativeLayout rlWebContainer;
    private WebView webView;
//    private WebViewProgressBar progressBar;

    public static final String TITLE = "title";
    public static final String URL = "url";

    private String webUrl = "";
    private String title = "";

    private ImageView webBack;
    private TextView webTitle, tvEmpty;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview2);
//        webUrl = getIntent().getExtras().getString(URL);
        webUrl = getIntent().getStringExtra("url");

//        try {
//            webUrl = URLDecoder.decode(owebUrl, "utf-8");
//        } catch (UnsupportedEncodingException e) {
//            throw new RuntimeException(e);
//        }

        Log.d("WebViewActivity", "===@@@===webUrl: "+webUrl);
//        title = getIntent().getExtras().getString(TITLE);
        initView();
        setListener();
//        setActivityTitle(title);
    }

    public void initView() {
        rlWebContainer = findViewById(R.id.rl_web_container);
        webView = findViewById(R.id.act_webView);
//        progressBar = findViewById(R.id.activity_webview_progressbar);
        webBack = findViewById(R.id.wv_back);
        webTitle = findViewById(R.id.wv_title);
        tvEmpty = findViewById(R.id.tv_empty);
    }

    /**
     * 设置页面标题
     */
    public void setActivityTitle(String title) {
//        mRootView.showTitle(title);
    }

    private void setWebTitle(String title) {
        webTitle.setText(title);
    }


    public void setListener() {
        tvEmpty.setOnClickListener(this);
        webBack.setOnClickListener(this);
        initWebSettings(webView);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        });

        webView.requestFocus();
        webView.setOnCreateContextMenuListener(this);
        webView.setFocusable(true);
        webView.loadUrl(webUrl);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebSettings(WebView webView){
        WebSettings webSetting = webView.getSettings();
        // 支持JavaScript
        webSetting.setJavaScriptEnabled(true);
        // 设置可以访问文件s
        webSetting.setAllowFileAccess(true);
        // 告诉javascript来自动打开的窗口。这适用于JavaScript函数的窗口，open()。
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        // 支持缩放
        webSetting.setSupportZoom(true);
        // 是否禁止是网络加载数据
        webSetting.setBlockNetworkLoads(false);
        // 设置是否支持多窗口
        webSetting.setSupportMultipleWindows(true);
        // 是否开启本地DOM存储
        webSetting.setDomStorageEnabled(true);
        // 设置不缓存
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 阻塞加载图片
        webSetting.setBlockNetworkImage(false);
        // 支持启用插件
        webSetting.setPluginState(WebSettings.PluginState.ON);

        //网页超出屏幕大小的处理
        webSetting.setUseWideViewPort(true);
        webSetting.setLoadWithOverviewMode(true);

        // 控制页面显示布局
        // NARROW_COLUMNS：可能的话使所有列的宽度不超过屏幕宽度
        // NORMAL：正常显示不做任何渲染
        // SINGLE_COLUMN：把所有内容放大webview等宽的一列中
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        //禁止用地理定位
        webSetting.setSaveFormData(true);
        // 是否启动地理定位
        webSetting.setGeolocationEnabled(true);
        // 设置定位的数据库路径
        webSetting.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 大于21
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    public WebView newWebView = null;

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.wv_back){
            if (webView.canGoBack()) {
                webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                webView.goBack();
            } else {
                finish();
            }
        } else if (v.getId() == R.id.tv_empty) {
            tvEmpty.setVisibility(View.GONE);
            webView.loadUrl(webUrl);
        }
    }


    //判断是否存在汉字
    public boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        LogUtil.d("CCC == webView.canGoBack() --> "+webView.canGoBack());
        if (null != newWebView) {
            LogUtil.d("CCC == newWebView.canGoBack() --> "+newWebView.canGoBack());
            if (newWebView.canGoBack()) {
                newWebView.goBack();
            } else{
                destroyNewWeb();
            }
        } else
            if (null != webView && webView.canGoBack()) {
            webView.goBack();
        } else {
            LogUtil.d("CCC == super.onBackPressed() ---");
            super.onBackPressed();
        }
    }

    private void destroyNewWeb(){
        if (newWebView != null) {
            newWebView.clearCache(true);
            newWebView.clearHistory();
            rlWebContainer.removeView(newWebView);
            newWebView.destroy();
            newWebView = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyNewWeb();
        if (webView != null) {
            webView.clearCache(true);
            webView.clearHistory();
            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
        }
    }

}
